//
//  RevertAnnucetementController.m
//  Village
//
//  Created by 程虎 on 2017/9/13.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "RevertAnnucetementController.h"
#import "AnnounModel.h"
#import "TZGGHttpTools.h"

@interface RevertAnnucetementController ()<UITextViewDelegate>
@property (nonatomic, strong) AnnounModel *model;
@property (nonatomic, copy) LongriseRouterBlock routerBlock;
@end

@implementation RevertAnnucetementController

- (void)setParams:(id)obj block:(LongriseRouterBlock)block
{
    self.model = (AnnounModel *)obj;
    self.routerBlock = block;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"通知回复";
    self.view.backgroundColor = kBackgroundColor;
    [self set_NavItem];
    [self config_UI];
}

#pragma mark - 设置导航栏左右按钮
- (void)set_NavItem
{
    UIButton *leftbtn = [[UIButton alloc] initWithFrame:CGRectMake(0,0,70,40)];
    [leftbtn setTitle:@"取消" forState:UIControlStateNormal];
    leftbtn.titleLabel.font = LRFont(15);
    leftbtn.titleEdgeInsets = UIEdgeInsetsMake(5, -40, 5, 0);
    [leftbtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftbtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIButton *rightbtn = [[UIButton alloc] initWithFrame:CGRectMake(0,0,70,40)];
    [rightbtn setTitle:@"确定" forState:UIControlStateNormal];
    rightbtn.titleLabel.font = LRFont(15);
    rightbtn.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, -40);
    [rightbtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightbtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)config_UI
{
    self.txtView.delegate = self;
}

#pragma mark - 设置
- (void)leftBtnClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 确定
- (void)rightBtnClick
{
    if (self.txtView.text.length) {
        [self updateReplay];
    }
    else {
        [MBProgressHUD showError:@"请输入回复内容！" toView:self.navigationController.view];
    }
}

- (void)textViewDidChange:(UITextView *)textView
{
    self.placeholderLab.hidden = textView.hasText;
}

#pragma mark - 消息回复
- (void)updateReplay
{
    [MBProgressHUD showMessage:@"正在上传..." toView:self.navigationController.view];
    WeakSelf(weakSelf);
    [TZGGHttpTools ReplayTZGGWithModel:self.model WithReplayStr:self.txtView.text Success:^(NSArray *valueAry) {
        [MBProgressHUD hideHUDForView:weakSelf.navigationController.view];
        [MBProgressHUD showSuccess:@"回复成功" toView:self.navigationController.view Completion:^{
            if (weakSelf.routerBlock) {
                weakSelf.routerBlock(@"");
            }
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }];
    } failure:^(NSString *error) {
        [MBProgressHUD hideHUDForView:weakSelf.navigationController.view];
    }];
}
@end
